<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col>
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 下面就是树形控件内容了 -->
        <el-tree :data="data" :props="defaultProps">
          <!-- 注意：node是抛出来的当前的节点信息，data是当前的节点数据 -->
          <treeTools slot-scope="{ data }" :tree-node="data"></treeTools>
        </el-tree>
      </el-card>
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools'
import { getDepartmentApi } from '@/api/department'
export default {
  components: {
    treeTools
  },
  data() {
    return {
      data: [
        {
          name: '一级 1',
          child: [
            {
              name: '二级 1-1',
              child: [
                {
                  name: '三级 1-1-1'
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        children: 'child',
        label: 'name'
      },
      list: [
        { id: '1-1', pid: '1', name: '1-1' },
        { id: '1-2', pid: '1', name: '1-2' },
        { id: '1-3', pid: '1', name: '1-3' },
        { id: '1', pid: '', name: '1' },
        { id: '2', pid: '', name: '2' },
        { id: '2-1', pid: '2', name: '2-1' },
        { id: '3', pid: '', name: '3' },
        { id: '3-1', pid: '3', name: '3-1' },
        { id: '3-2', pid: '3', name: '3-2' },
        { id: '3-2-1', pid: '3-2', name: '3-2-1' }
      ],
      // res: [
      //   {
      //     id: '1',
      //     pid: '',
      //     children: [
      //       { id: '1-1', pid: '1', name: '1-1' },
      //       { id: '1-2', pid: '1', name: '1-2' },
      //       { id: '1-3', pid: '1', name: '1-3' }
      //     ]
      //   },
      //   {
      //     pid: '',
      //     id: '2',
      //     children: [{ id: '2-1', pid: '2', name: '2-1' }]
      //   },
      //   {
      //     id: '3',
      //     pid: '',
      //     children: [
      //       { id: '3-1', pid: '3', name: '3-1' },
      //       { id: '3-2', pid: '3', name: '3-2', children: [
      //         { id: '3-2-1', pid: '3-2', name: '3-2-1' }
      //       ] }
      //     ]
      //   }
      // ]
    }
  },
  created() {
    this.initData()
    // console.log(this.transTreeData(this.list, ''), '=============');
  },
  methods: {
    // 定义一个方法，获取指定pid的数据
    transTreeData(list, pid) {
      const arr = []
      list.forEach(item => {
        if (item.pid === pid) {
          // 递归调用一下自己，检查一下有没有自己的子集数据
          const children = this.transTreeData(list, item.id)
          if (children.length > 0) {
            item.children = children
          }
          arr.push(item)
        }
      })
      return arr
    },
    // 初始化部门列表数据
    async initData() {
      const res = await getDepartmentApi()
      // console.log(res)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
